//引用腾讯地图API
var QQMapWX = require('../../libs/qqmap-wx-jssdk1.2.js');
var qqmapsdk;
const app = getApp()
Page({
  data: {
    title: {
      "flag": 1,
      "name": "选择地址"
    },
    address: "",
    src: "",
     latitude: 0,//地图初次加载时的纬度坐标
    longitude: 0, //地图初次加载时的经度坐标
    name: "", //选择的位置名称
    id:'',
  },
  onChangeAddress: function (e) {
    this.moveToLocation();
  },
  onLoad: function (options) {
    this.lookup(options);
    this.getsuggest();
    
  },
  onShow: function (options) {
  

  },
  lookup: function (options){
    if (options.address != null && options.address != '') {
      //设置变量 address 的值
      this.setData({
        address: options.address
      });
    } else {
      qqmapsdk = new QQMapWX({
        //此key需要用户自己申请
        key: 'IM6BZ-HZORR-TRQW6-WUC5W-SDAWE-W4F5Q'

      });
      var that = this;
      qqmapsdk.reverseGeocoder({
        success: function (res) {
          var refFarea=null;
          that.data.latitude= res.result.location.latitude,
          that.data.longitude= res.result.location.longitude,
              refFarea = res.result.address_reference.famous_area;
            if (refFarea == null) { refFarea = res.result.address_reference.landmark_l2 }
           if (refFarea == null) { refFarea = res.result.address_reference.landmark_l1 }
          that.data.id = refFarea.id
          that.data.address = refFarea.title
          that.data.latitude = refFarea.location.lat
          that.data.longitude = refFarea.location.lng
          that.setData({
            address: refFarea.title
          });
        },
        fail: function (res) {
          //console.log(res);
        },
        complete: function (res) {
          //console.log(res);
        }
      })
    }
  },
  moveToLocation: function () {
    var that = this;
    wx.chooseLocation({
      success: function (res) {
        that.data.latitude = res.latitude,
          that.data.longitude = res.longitude
        that.onLoad(res)
      },
      fail: function (err) {
        console.log(err)
      }
    });
  },
  //触发关键词输入提示事件
  getsuggest: function (e) {
    var _this = this;
    let kword = '';
    if (e == null || e == '' || e == undefined) {
      kword = "购物广场";
    } else {
      kword = e.detail.value;
    }
    //调用关键词提示接口
    qqmapsdk.search({
      //获取输入框值并设置keyword参数
      keyword: kword, //用户输入的关键词，可设置固定值,如keyword:'KFC'  e.detail.value
      //  region: '上海', //设置城市名，限制关键词所示的地域范围，非必填参数
      page_size: 8,
      success: function (res) {//搜索成功后的回调
        var sug = [];
        for (var i = 0; i < res.data.length; i++) {
          console.log(res.data[i].id)
          sug.push({ // 获取返回结果，放到sug数组中
            title: res.data[i].title,
            id: res.data[i].id,
            addr: res.data[i].address,
            city: res.data[i].ad_info.city,
            district: res.data[i].ad_info.district,
            latitude: res.data[i].location.lat,
            longitude: res.data[i].location.lng,
            province: res.data[i].ad_info.province,
          });
        }
        _this.setData({
          showview: false,
        })
        _this.setData({ //设置suggestion属性，将关键词搜索结果以列表形式展示
          suggestion: sug

        });
      },
      fail: function (error) {
        console.error(error + "失败");
        _this.setData({
          showview: true
        })
      },
      complete: function (res) {
      //  console.log(res);

      }
    });
  },
  backadds:function(){
    let pages = getCurrentPages();  // 当前页的数据，可以输出来看看有什么东西
    let prevPage = pages[pages.length - 2];  // 上一页的数据，也可以输出来看看有什么东西
    /** 设置数据 这里面的 value 是上一页你想被携带过去的数据，后面是本方法里你得到的数据，我这里是detail.value，根据自己实际情况设置 */
    prevPage.setData({
      latitude: this.data.latitude,
      longitude: this.data.longitude,
      id: this.data.id,
      address: this.data.address,
    })
    /** 返回上一页 这个时候数据就传回去了 可以在上一页的onShow方法里把 value 输出来查看是否已经携带完成 */
    wx.navigateBack({});
  },
  //方法回填
  backfill: function (e) {
    this.setData({
      showview: true
    })
    var id = e.currentTarget.id;
    for (var i = 0; i < this.data.suggestion.length; i++) {
      if (i == id) {
        let pages = getCurrentPages();  // 当前页的数据，可以输出来看看有什么东西
        let prevPage = pages[pages.length - 2];  // 上一页的数据，也可以输出来看看有什么东西
        /** 设置数据 这里面的 value 是上一页你想被携带过去的数据，后面是本方法里你得到的数据，我这里是detail.value，根据自己实际情况设置 */
        var refFarea=  this.data.suggestion[i];
        prevPage.setData({
          marketinfo:{
            marketId : refFarea.id,
            marketName: refFarea.title,
            latitude: refFarea.latitude,
            longitude: refFarea.longitude,
            province: refFarea.province || '',
            city: refFarea.city || '',
            district: refFarea.district || '',
            address:refFarea.addr,
          },
          latitude: refFarea.latitude,
          longitude: refFarea.longitude,
          id: refFarea.id,
          address: refFarea.title,
        })
        /** 返回上一页 这个时候数据就传回去了 可以在上一页的onShow方法里把 value 输出来查看是否已经携带完成 */
        wx.navigateBack({});
        return
      }
    }
  },
  // 根据关键词搜索附近位置
  nearby_search: function () {
    var self = this;
    // 调用接口
    qqmapsdk.search({
      keyword: self.data.detail,  //搜索关键词
      //boundary: 'nearby(' + self.data.latitude + ', ' + self.data.longitude + ', 1000, 16)',
      location: self.data.latitude + ',' + self.data.longitude,
      page_size: 20,
      page_index: 1,
      success: function (res) { //搜索成功后的回调
        //console.log(res.data)
        var sug = [];
        for (var i = 0; i < res.data.length; i++) {
          sug.push({ // 获取返回结果，放到sug数组中
            title: res.data[i].title,
            id: res.data[i].id,
            addr: res.data[i].address,
            province: res.data[i].ad_info.province,
            city: res.data[i].ad_info.city,
            district: res.data[i].ad_info.district,
            latitude: res.data[i].location.lat,
            longitude: res.data[i].location.lng
          });
        }
        self.setData({
          selectedId: 0,
          nearList: sug,
          suggestion: sug
        })
        self.addMarker(sug[0]);
      },
      fail: function (res) {
        //console.log(res);
      },
      complete: function (res) {
        //console.log(res);
      }
    });
  },
  focus:function(e){
    this.setData({
      inputShowed:true　　　
        })
  },
  blur: function (e) {
    // this.setData({
    //   inputShowed:false        
    // })
  },
})